﻿using CheatMenu.Managers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
using CheatMenu.Utils;
using BepInEx.Logging;
using Logger = BepInEx.Logger;

namespace CheatMenu.Menus
{
    public class MenuPromtKeyBind : MenuBase
    {
        public delegate void KeyBoundDelegate(MenuPromtKeyBind sender, KeyCode keyPressed);

        public event KeyBoundDelegate KeyBoundEvent;
        protected void OnKeyBound(KeyCode pressedButton)
        {
            Shown = false;
            KeyBoundEvent?.Invoke(this, pressedButton);
        }

        private GUIStyle defaultFontLabelStyle;
        private GUIStyle defaultFontLabelHeaderStyle;
        private GUIStyle defaultFontControlWidthHeightButtonStyle;

        public string BindName { get; set; }

        public MenuPromtKeyBind(string name, string text = null) : base(name, text)
        {
            Alignment = Alignment.Centre;
        }

        protected override Vector2 InitializeSize()
        => new Vector2((GUIManager.ControlWidth * 3) + (GUIManager.PaddingSize * 2), (GUIManager.ControlHeight * 2) + (GUIManager.PaddingSize * 2));

        protected override void InitializeGUI()
        {
            defaultFontLabelStyle = new GUIStyle(GUI.skin.label)
            {
                fontSize = GUIManager.ControlFontSize,
                normal = new GUIStyleState
                {
                    textColor = GUIManager.LabelColor
                }
            };

            defaultFontLabelHeaderStyle = new GUIStyle(GUI.skin.label)
            {
                fontSize = GUIManager.ControlFontSize,
                fontStyle = FontStyle.Bold,
                normal = new GUIStyleState
                {
                    textColor = GUIManager.LabelHeaderColor
                }
            };

            defaultFontControlWidthHeightButtonStyle = new GUIStyle(GUI.skin.button)
            {
                fontSize = GUIManager.ControlFontSize,
                fixedWidth = GUIManager.ControlWidth,
                fixedHeight = GUIManager.ControlHeight
            };
        }

        protected override void MenuWindow(int id)
        {
            GUILayout.BeginVertical();
            {
                GUILayout.BeginVertical(GUI.skin.box);
                {
                    GUILayout.Label("Hit any key for new keybind:", defaultFontLabelHeaderStyle);
                    if (!string.IsNullOrEmpty(BindName)) GUILayout.Label(BindName, defaultFontLabelStyle);
                }
                GUILayout.EndVertical();
            }
            GUILayout.EndVertical();

            Event e = Event.current;
            if (e.rawType == EventType.KeyDown && e.keyCode != KeyCode.None)
            {
                OnKeyBound(e.keyCode);
            }
        }

        public void PopulateAndShow(string bindName)
        {
            BindName = bindName;
            Focus();
            BringWindowToFront();
            Shown = true;
        }
    }
}
